//
//  GradeTableViewController.m
//  hello mui
//
//  Created by zhangtao on 15/9/16.
//  Copyright (c) 2015年 zt.td. All rights reserved.
//

#import "GradeTableViewController.h"
@interface GradeTableViewController ()
@property(nonatomic,strong)NSString *result;
@property(nonatomic,strong)UIImageView *imageIV;
@property(nonatomic,assign)NSInteger selectCell;
@end
@implementation GradeTableViewController

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = VIEWCO;
    if([NSString stringWithFormat:@"%@", NSUSER_DEF(@"SELECTVALUE")].length != 0){
        NSString *sele = [NSString stringWithFormat:@"%@", NSUSER_DEF(@"SELECTVALUE")];

        _selectCell = [sele intValue];
        self.tableView.separatorStyle = NO;
    }else{
        _selectCell = -1;
    }

    
    if (_passValue == 0) {
        self.title = @"年级";

    }else if (_passValue ==4){
        self.title = @"学期";
    }

    
    
    
  
    [self setExtraCellLineHidden:self.tableView];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (_array == nil) ? 0 : _array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCell = @"CustomCell";
    UINib *nib = [UINib nibWithNibName:@"GradeTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CustomCell];
    
    
    GradeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCell forIndexPath:indexPath];
    cell.textLabel.text = _array[indexPath.row];
    
    if (_selectCell>=0) {
        
        if (_selectCell == indexPath.row) {
             _imageIV = [[UIImageView alloc]initWithFrame:CGRectMake(MAIN_WIDTH-40, 7, 25, 25)];
             _imageIV.image = [UIImage imageNamed:@"item_selected.png"];
                [cell addSubview:_imageIV];
        }
    }
    
    if (indexPath.row == 0 ) {
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(215, 215, 215, 1);
        [cell addSubview:topL];
        
    }else if(indexPath.row == (_array.count-1)) {
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(220, 220, 220, 1);
        [cell addSubview:topL];
        
//        cell.layer.borderWidth = 0;
//        cell.layer.borderColor = [[UIColor redColor]CGColor];//[RGB(210, 210, 210, 1)CGColor];
        UILabel *headL = [[UILabel alloc]initWithFrame:CGRectMake(0, 39, MAIN_WIDTH, 1)];
        headL.backgroundColor = RGB(215, 215, 215, 1);
        [cell addSubview:headL];
    }else{
        UILabel *topL = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, MAIN_WIDTH, 1)];
        topL.backgroundColor = RGB(220, 220, 220, 1);
        [cell addSubview:topL];
    }
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _result = _array[indexPath.row];

    NSString *keyName;
    NSString *keyValue;
    
    if (_passValue == 0) {
        _change.gradeLabel.text = _result;
        NSUSER_DEF_NORSET(_change.gradeLabel.text, @"GRADE");
        keyName = @"gradeName";
    }else if (_passValue ==4){
        _change.dateStuLabel.text = _result;
        NSUSER_DEF_NORSET(_change.dateStuLabel.text, @"DATA");
        keyName = @"termName";
    }
    keyValue = [NSString stringWithFormat:@"%@",_result];
    
    _selectCell = indexPath.row;
    
    NSString *str = [NSString stringWithFormat:@"%ld",indexPath.row];
    
    NSUSER_DEF_NORSET(str, @"SELECTVALUE");
    
    [_imageIV removeFromSuperview];
    [self request:keyName def:keyValue];
    
    [self.tableView reloadData];
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)request:(NSString *)str def:(NSString *)val{
    NSString *path = @"personalCenter/updateReadInfo";
    NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
    
    
    NSDictionary *dic = @{@"userId":userId,str:val};
    NSDictionary *dics =  @{@"userId":userId,str:val,@"bd_sig":[ToolObjc md5Str:dic]};
    
    [[HttpConnctionManager sharedInstance]requestParam:dics andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        NSLog(@"~%@~~%@",[succeedResult valueForKey:@"resCode"],[succeedResult valueForKey:@"resMessage"]);
        [ToolObjc showMessage:@"修改成功"];
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        
    }];
    dic = nil;

}
@end
